package org.example.library.config;

import lombok.extern.slf4j.Slf4j;
import org.example.library.entity.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handler(Exception e){
        log.error("发生异常, e:", e);
        return Result.fail("内部错误, 请联系管理员");
    }

    @ExceptionHandler
    public Result handler(NullPointerException e){
        log.error("发生异常, e:", e);
        return Result.fail(-2, "发生空指针异常");
    }

    @ExceptionHandler
    public Result handler(ArithmeticException e){
        log.error("发生异常, e:", e);
        return Result.fail(-3, "发生算术异常");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result handler2(Exception e){
        log.error("发生异常, e:", e);
        return Result.fail(-4, "发生数组越界异常");
    }
}
